<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;


class Config extends Backend
{
    
    public function __construct(){

        //继承父类
        parent::__construct();

        $this->ConfigModel = model('Config');
    
    }   
    
    // 配置列表页
    public function index()
    {
        $count = $this->ConfigModel->count();

        $countlist = $this->ConfigModel->paginate(10);

        $pagestr = $countlist->render();

        // var_dump($countlist);
        // exit;

        $this->assign([
            'count' => $count,
            'countlist' => $countlist,
            'pagestr' => $pagestr
        ]);

        return $this->fetch();
    }

    // 添加
    public function add()
    {   
        if($this->request->isPost()){

            $title = $this->request->param('title', '');
            $key = $this->request->param('key', '');
            $type = $this->request->param('type', '');

            if($type == 'text'){
                $value = $this->request->param('value', '');
                
            } else{

                // 图片上传
                $valueUrl = upload('value');

                if($valueUrl === FALSE)
                {
                    $this->error('文件上传失败');
                    exit;
                }

                $value = '/uploads/'.$valueUrl;
            }

            $data = [
                'title' => $title,
                'key' => $key,
                'type' => $type,
                'value' => $value,
            ];

            

            $result = $this->ConfigModel->save($data);

            
            if($result === FALSE){

                //上传上来的图片删除了
                @is_file($data['value']) && @unlink($data['value']);

                $this->error($this->ConfigModel->getError());
                exit;
            } else{

                $this->success('配置信息添加成功', 'admin/config/index');
                exit;
            }
        }
        return $this->fetch();
    }

    public function edit() 
    {   
        $id = $this->request->param('id', 0);

        $configinfo = $this->ConfigModel->find($id);

        $this->assign('info',$configinfo);

        if($this->request->isPost()){

            $params = $this->request->param();
            
            if($params['type'] == 'text'){

                $params['value'] = $this->request->param('value', '');
                
            } else{

                if($_FILES["value"]["size"] > 0){
                    // 图片上传
                    $valueUrl = upload('value');

                    if($valueUrl === FALSE)
                    {
                        $this->error('文件上传失败');
                        exit;
                    }

                    $params['value'] = '/uploads/'.$valueUrl;
                }
            }
            $data = [
                'id' =>$params['id'],
                'title' =>$params['title'],
                'key' =>$params['key'],
                'type' =>$params['type'],
                'value' =>$params['value']
            ];
            $result = $this->ConfigModel->isUpdate()->save($data);

            if($result === FALSE){

                if($params['type'] == "file" && $_FILES['value']['size'] > 0){
                    //上传上来的图片删除了
                    @is_file($params['value']) && @unlink($params['value']);
                }

                $this->error($this->ConfigModel->getError());
                exit;

            } else{
                
                if($params['type'] == "file" && $_FILES['value']['size'] > 0){
                    //修改成功后，将原来的图片删除
                    $avatar = ltrim($configinfo['value'], '/');
                    @is_file($avatar) && @unlink($avatar);
                }

                $this->success('配置信息修改成功', 'admin/config/index');
                exit;
            }
        }
        
        return $this->fetch();
    }

    public function del()
    {
        if($this->request->isPost()){

            $success = [
                'result' => false,
                'msg' => ''
            ];

            $ids = $this->request->param('ids', 0);

            $result = $this->ConfigModel->destroy($ids);


            if($result === FALSE)
            {
                $success['result'] = false;
                $success['msg'] = $this->ConfigModel->getError();
            }else
            {
                $success['result'] = true;
                $success['msg'] = '删除配置成功';
            }

            return json($success);
        }
    }
}